package com.repast.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.Const;
import com.repast.common.ServerResponse;
import com.repast.dao.AddDishMapper;
import com.repast.dao.AppointMapper;
import com.repast.dao.DeskMapper;
import com.repast.dao.DeskTimeMapper;
import com.repast.dao.FoodsMapper;
import com.repast.dao.OrdersItemMapper;
import com.repast.dao.OrdersMapper;
import com.repast.dao.RestaurantInfoMapper;
import com.repast.pojo.AddDish;
import com.repast.pojo.Appoint;
import com.repast.pojo.Desk;
import com.repast.pojo.DeskTime;
import com.repast.pojo.Foods;
import com.repast.pojo.Orders;
import com.repast.pojo.OrdersItem;
import com.repast.pojo.RestaurantInfo;
import com.repast.service.IAppointService;
import com.repast.utils.CalculateDateUtils;
import com.repast.utils.DateUtils;
import com.repast.vo.AddDishItemVo;
import com.repast.vo.AppointDeskVo;
import com.repast.vo.AppointOrdersInfo;
import com.repast.vo.OrdersItemVo;

@Service
public class AppointServiceImpl implements IAppointService {

	@Autowired
	private AppointMapper appointMapper;
	@Autowired
	private DeskTimeMapper deskTimeMapper;
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private DeskMapper deskMapper;
	@Autowired
	private OrdersItemMapper ordersItemMapper;
	@Autowired
	private AddDishMapper addDishMapper;
	@Autowired
	private FoodsMapper foodsMapper;
	@Autowired
	private RestaurantInfoMapper rsMapper;

	public ServerResponse makeAppoint(Appoint appoint) throws Exception {

		// 封装状态,已预约
		appoint.setAppointUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
		appoint.setAppointStatus(Const.OrdersTypeStatus.EVER_APPOINT);
		appointMapper.insert(appoint);
		// 封装预约的结束时间
		String assignAddTime = CalculateDateUtils.assignAddTime(appoint.getAppointCreateTime(), 30);
		DeskTime deskTime = new DeskTime();
		deskTime.setDeskTimeStartTime(appoint.getAppointCreateTime());
		deskTime.setDeskTimeEndTime(assignAddTime);
		deskTime.setDeskId(appoint.getDeskId());
		deskTime.setAppointId(appoint.getAppointId());
		deskTime.setDeskCreateTime(DateUtils.formatDateTime(new Date(), "long"));
		deskTime.setDskUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
		int insertDeskTime = deskTimeMapper.insert(deskTime);
		if (insertDeskTime == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.DESK_TIME_ERROR, "桌子预定时间生成失败");
		}
		return ServerResponse.createBySuccess(appoint.getAppointId());
	}

	public ServerResponse abolishAppoint(Integer appointId) {
		Appoint appoint = appointMapper.selectByPrimaryKey(appointId);
		
		//取消预约,修改相关的订单状态为已取消
		Integer ordersId = appoint.getOrdersId();
		if (ordersId != null) {
			Orders orders = ordersMapper.selectByPrimaryKey(ordersId);
			orders.setOrdersTypeStatus(Const.OrdersTypeStatus.EVER_ABOLISH);
			orders.setOrdersUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
			int result = ordersMapper.updateByPrimaryKeySelective(orders);
			if (result == 0) {
				return ServerResponse.createByErrorCodeMessage(Const.NO_APPOINT_ORDERS, "没有找到预约相关联的订单");
			}
		}
		appoint.setAppointUpdateTime(DateUtils.formatDateTime(new Date(), "long"));
		appoint.setAppointStatus(Const.OrdersTypeStatus.EVER_ABOLISH);
		int update = appointMapper.updateByPrimaryKeySelective(appoint);
		if (update == 0) {
			return ServerResponse.createByErrorCodeMessage(Const.ABOLISH_APPOINT_FAIL, "系统异常,取消预约失败,请联系店家");
		}
		//删除预约的时间
		int resultDelete = deskTimeMapper.deleteDeskTimeByAppointId(appoint.getAppointId());
		if(resultDelete==0){
			return ServerResponse.createByErrorCodeMessage("删除时间失败");
		}
		
		return ServerResponse.createBySuccess();
	}

	public ServerResponse judgeAppoint(String openid) throws Exception {
		// 查询最后一次预约开始的时间
		Appoint appoint = appointMapper.selectMaxIdAppointByOpenid(openid);
		// lastAppointStartTime==null 没有任何预约信息
		// 把String转化为时间
		if(appoint!=null){
			Integer ordersId = appoint.getOrdersId();
			if(ordersId==null){
				AppointDeskVo appointDeskVo = new AppointDeskVo();
				Desk desk = deskMapper.selectDeskInfo(appoint.getDeskId());
				appointDeskVo.setDeskName(desk.getDeskName());
				appointDeskVo.setDeskType(desk.getDeskType());
				appointDeskVo.setAppoint(appoint);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date lastStartTime = sdf.parse(appoint.getAppointCreateTime());
				Date date = new Date();
				if (date.before(lastStartTime)) {
					return ServerResponse.createByErrorCodeMessage(appointDeskVo);
				}
			}
			if(ordersId!=null){
				AppointOrdersInfo info = new AppointOrdersInfo();
				
				AppointDeskVo appointDeskVo = new AppointDeskVo();
				Desk desk = deskMapper.selectDeskInfo(appoint.getDeskId());
				appointDeskVo.setDeskName(desk.getDeskName());
				appointDeskVo.setDeskType(desk.getDeskType());
				appointDeskVo.setAppoint(appoint);
				Orders orders = ordersMapper.selectByPrimaryKey(ordersId);
				//如果预约已订单的展示订单的信息
				//订单小项的信息
				List<OrdersItemVo> ordersItemVoList = new ArrayList<OrdersItemVo>();
				List<OrdersItem> ordersItemList = ordersItemMapper.selectOrderItemListByOrdersId(ordersId);
				for (OrdersItem ordersItem : ordersItemList) {
					OrdersItemVo ordersItemVo = new OrdersItemVo();
					Foods foods = foodsMapper.selectByPrimaryKey(ordersItem.getFoodsId());
					ordersItemVo.setFoodsId(ordersItem.getFoodsId());
					ordersItemVo.setFoodsName(foods.getFoodsName());
					ordersItemVo.setFoodsPrice(foods.getFoodsPrice());
					ordersItemVo.setOrdersItemNum(ordersItem.getOrdersItemNum());
					ordersItemVo.setOrdersItemSubtotal(ordersItem.getOrdersItemSubtotal());
					ordersItemVoList.add(ordersItemVo);
				}
				//加菜小项信息
				List<AddDish> addDishList = addDishMapper.selectAddDishItemListByOrdersId(ordersId);
				List<AddDishItemVo> addDishItemVoList = new ArrayList<AddDishItemVo>();
				for (AddDish addDish : addDishList) {
					AddDishItemVo addDishItemVo = new AddDishItemVo();
					Foods foods = foodsMapper.selectByPrimaryKey(addDish.getFoodsId());
					addDishItemVo.setFoodsId(foods.getFoodsId());
					addDishItemVo.setFoodsName(foods.getFoodsName());
					addDishItemVo.setFoodsPrice(foods.getFoodsPrice());
					addDishItemVo.setAddDishItemNum(addDish.getAddDishNum());
					addDishItemVo.setAddDishItemSubtotal(addDish.getAddDishSubtotal());
					addDishItemVoList.add(addDishItemVo);
				}
				info.setOrdersItemList(ordersItemVoList);
				info.setAddDishItemList(addDishItemVoList);
				info.setOrders(orders);
				info.setAppointDeskVo(appointDeskVo);
				return ServerResponse.createByErrorCodeMessage(Const.APPOINT_ORDERS_PAY, "这是预约已点餐", info);
			}
			
		}
		
		
		List<RestaurantInfo> list = rsMapper.selectRestaurant();
		List<String> weekList = new ArrayList();
		if (list.size() != 0) {
			for (RestaurantInfo restaurantInfo : list) {
				String day = restaurantInfo.getRestaurantInfoDay();
				String[] days = day.split(",");
				String str[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };// 字符串数组
				for (String string : days) {
					weekList.add(str[(Integer.parseInt(string) - 1)]);
				}
 			}
		}
		return ServerResponse.createBySuccess(weekList);
	}

}
